#!/bin/sh -e

#
# ejabberd now handles domain changes in the initrd script
#
SERVICE_NAME=ejabberd-xs

CONFIG_LIST="/etc/ejabberd/ejabberd-xs.cfg"

# taken from ejabberd spec %post
# taken from ejabberd spec %post
#function do-cert(){
#    (cd /etc/ejabberd
#    if [ ! -f ejabberd.pem ]
#    then	
#	echo "Generating SSL certificate /etc/ejabberd/ejabberd.pem..."
#	HOSTNAME=$(hostname -s)
#	DOMAINNAME=$(hostname -d)
#	openssl req -new -x509 -days 36500 -nodes -out ejabberd.pem -keyout ejabberd.pem > /dev/null 2>&1 << +++
#	.
#	.
#	.
#	$DOMAINNAME
#	$HOSTNAME
#	ejabberd
#	root@$HOSTNAME.$DOMAINNAME
#	+++
#	chown ejabberd:ejabberd ejabberd.pem
#	chmod 600 ejabberd.pem
#    fi)
#}

#  This is the suffix which original versions of modified files will have
BACKUP_SUFFIX=old

short_host=`hostname -s`
new_name=$short_host.$1

for config in $CONFIG_LIST;
do
    if [ -e $config.in ]; then
	if [ -e $config ]; then
	    mv $config $config.$BACKUP_SUFFIX
	fi
	sed -e s/{{ iiab_hostname }}/$new_name/ $config.in > $config ;
    else
	echo WARNING: Skipped $config - template file is missing!
    fi
done

#if [ -e /etc/ejabberd/ejabberd.pem.$1 ]; then
#    rm /etc/ejabberd/ejabberd.pem.$1	
#fi
#mv /etc/ejabberd/ejabberd.pem /etc/ejabberd/ejabberd.pem.$1
#do-cert

# Since for the community edition, we don't really expect all modules to be present
#   Just exit, and expect the user to do a restart

exit 0


